Form_DiscountManagement = function(){
	var obj = this;

	var storeSchool = new Ext.data.Store({
		proxy : new Ext.data.HttpProxy({url : "/map/getSchools"}),
		reader : new Ext.data.ArrayReader({
			fields : ["id", "name"]
		})
	});
	var comboSchool = new Ext.form.ComboBox({
		fieldLabel : "学校",
		name : "school",
		emptyText : "请选择学校...",
		valueField : "id",
		displayField : "name",
		editable : false,
		triggerAction : "all",
		store : storeSchool
	});

	var storeSchoolMapProxy = new Ext.data.HttpProxy({url : "/map/getSchoolMapsGoogleBySchoolId"});
	var storeSchoolMap = new Ext.data.Store({
		proxy : storeSchoolMapProxy,
		reader : new Ext.data.ArrayReader({
			fields : ["id", "name", "zoom", "posX", "posY"]
		})
	});
	var comboSchoolMap = new Ext.form.ComboBox({
		fieldLabel : "学校地图",
		name : "schoolMap",
		emptyText : "请选择学校地图...",
		valueField : "id",
		displayField : "name",
		editable : false,
		triggerAction : "all",
		store : storeSchoolMap
	});

	var storeSchoolMapLocProxy = new Ext.data.HttpProxy({url : "/map/getStoresByGoogleMapId"});
	var storeSchoolMapLoc = new Ext.data.Store({
		proxy : storeSchoolMapLocProxy,
		reader : new Ext.data.ArrayReader({
			fields : ["id", "name", "zoom", "posX", "posY"]
		})
	});
	var comboSchoolMapLoc = new Ext.form.ComboBox({
		fieldLabel : "店铺",
		name : "id",
		emptyText : "请选择店铺...",
		hiddenName : "id",
		valueField : "id",
		displayField : "name",
		editable : false,
		triggerAction : "all",
		store : storeSchoolMapLoc
	});

	var storeDiscountProxy = new Ext.data.HttpProxy({
		url : "/map/getDiscountByStoreId"
	});

	var storeDiscount = new Ext.data.Store({
		proxy : storeDiscountProxy,
		reader : new Ext.data.ArrayReader({
			fields : ["id", "name", "createTime", "validTime", "expireTime", "smallPic", "largePic"]
		})
	});

	var smDiscount = new Ext.grid.CheckboxSelectionModel({
		listeners: {
			selectionchange: function(sm) {
				if (sm.getCount()) listDiscount.removeButton.enable();
				else listDiscount.removeButton.disable();
			}
		}
	});

	var listDiscount = new Ext.grid.GridPanel({
		store : storeDiscount,
		height : 250,
		columns : [
			{id : "id", header : "ID", dataIndex : "id", width : 50},
			{id : "name", header : "优惠内容", dataIndex : "name", width : 500},
			{id : "validTime", header : "开始时间", dataIndex : "validTime", width : 100},
			{id : "expireTime", header : "结束时间", dataIndex : "expireTime", width : 100},
			{id : "createTime", header : "创建时间", dataIndex : "createTime", width : 100}
		],
		tbar : [{
			text : "删除选中优惠",
			iconCls : "remove",
			ref: "../removeButton",
			disabled : true,
			listeners : {
				click : function(){
					Ext.Msg.show({
						title : "确认",
						msg : "确认删除此优惠：" + smDiscount.getSelected().data.name + "？",
						buttons : Ext.MessageBox.YESNO,
						fn : function(button, text, opt){
							if (button == "yes"){
								Ext.Ajax.request({
									url: "/manage/removeDiscount",
									params : {id : smDiscount.getSelected().data.id},
									method : "GET",
									success:function(response, options){
										var data = Ext.util.JSON.decode(response.responseText);
										if (data == "success"){
											Ext.Msg.alert("提示", "删除成功！");
											storeDiscount.load();
										}  else {
											Ext.Msg.alert("错误", "错误：" + data + "。");
										}  
									},
									failure : function(){
										Ext.Msg.alert("错误", "无法连接到服务器。");
									}
								});
							}
						}
					});
				}
			}
		}],
		sm : smDiscount
	})
	
	storeSchoolMapProxy.addListener("beforeload", function(proxy, params){
		proxy.setUrl("/map/getSchoolMapsGoogleBySchoolId?id=" + comboSchool.getValue());
	});
	storeSchoolMapLocProxy.addListener("beforeload", function(proxy, params){
		proxy.setUrl("/map/getStoresByGoogleMapId?id=" + comboSchoolMap.getValue());
	});
	storeDiscountProxy.addListener("beforeload", function(proxy, params){
		proxy.setUrl("/map/getDiscountByStoreId?id=" + comboSchoolMapLoc.getValue());
	});
	comboSchool.addListener("select", function(combo, record, index){
		storeSchoolMap.removeAll();
		comboSchoolMap.setValue("");
		storeSchoolMapLoc.removeAll();
		comboSchoolMapLoc.setValue("");
		storeSchoolMap.load();
	});
	comboSchoolMap.addListener("select", function(combo, record, index){
		storeSchoolMapLoc.removeAll();
		comboSchoolMapLoc.setValue("");
		storeSchoolMapLoc.load();
	});
	comboSchoolMapLoc.addListener("select", function(combo, record, index){
		storeDiscount.load();
	});
	
	var inputName = new Ext.form.TextField({
		fieldLabel : "优惠内容",
		xtype : "textfield",
		name : "name",
		inputType : "text",
		allowBlank : false
	});
	var inputValidTime = new Ext.form.DateField({
		fieldLabel: "开始时间",
		xtype : "datefield",
		name : "validTime",
                format : "Y-m-d",
		allowBlank : false
	});
	var inputExpireTime = new Ext.form.DateField({
		fieldLabel: "结束时间",
		xtype : "datefield",
		name : "expireTime",
		format : "Y-m-d",
		allowBlank : false
	});
	var fieldSetMapLoc = new Ext.form.FieldSet({
		layout : "toolbar",
		items : [comboSchool, {xtype: "tbspacer", width: 10}, comboSchoolMap, {xtype: "tbspacer", width: 10}, comboSchoolMapLoc]
	});
	var fieldSetAddDiscount = new Ext.form.FieldSet({
		title : "新增优惠",
		items : [inputName, inputValidTime, inputExpireTime]
	});
	
	Form_DiscountManagement.superclass.constructor.call(this, {
		bodyStyle: "padding: 5px 5px 0",
		items : [fieldSetMapLoc, listDiscount, {xtype: "tbspacer", height: 10}, fieldSetAddDiscount],
		buttons : [{
			text : "增加",
			handler : function(){
				if (obj.getForm().isValid()){
					Ext.Ajax.request({
						url: "/manage/addDiscount",
						params : {name : obj.getForm().getFieldValues().name, validTime : obj.getForm().getFieldValues().validTime,
							  expireTime : obj.getForm().getFieldValues().expireTime, id : comboSchoolMapLoc.getValue()},
						method : "POST",
						success:function(response, options){
							var data = Ext.util.JSON.decode(response.responseText);
							if (data == "success"){
								Ext.Msg.alert("优惠管理", "添加成功");
								storeDiscount.load();
							}
							if (data == "error"){
								Ext.Msg.alert("优惠管理", "添加失败");
							}
						},
						failure : function(){
							Ext.Msg.alert("错误", "获取信息错误...");
						}
					});
				}
			}
		}]
	});
}


Ext.extend(Form_DiscountManagement, Ext.form.FormPanel, {
});
Ext.reg("App.MapManagement.DiscountManagement", Form_DiscountManagement);
